﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.IN;
using HiStaff.Domain.Sys;
using HiStaff.Util;
using HiStaff.Dal;
using Janus.Windows.GridEX;
using System.Data;
using System.Windows.Forms;
using HiStaff.Domain.HU;

namespace HiStaff.iProfile
{
    public partial class Accident : FormBase
    {
        public Accident()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            try
            {
                AddBasicCommandBar();
                return base.FormLoad();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_EMPLOYEE>("PRS_HU_EMPLOYEE", new EMPLOYEE_SEARCH { ORGID = trvOrg.OrgId, TERMINATE = chkTerminate.Value });
                grdEmpList.DataSource = lstEmployee;
                grdEmpList.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        private void trvOrg_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        protected override bool SaveButtonClick()
        {
            try
            {
                var objSave = new HU_WELFARE();
                ValueControlsToObject(objSave, this);
                //objSave.FAMILYID = ((HU_FAMILY)hiMultiColumnComboBox1.SelectedItem).ID;
                //objSave.EMPLOYEEID = ((HU_EMPLOYEE)grdEmpList.SelectedItems[0].GetRow().DataRow).ID;
                OracleHelper.ExcuteNonQuery("APRU_HU_WELFARE", objSave);
                DataAccident();
                MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        // load len Gridmain
        private void DataAccident()
        {
            try
            {
                decimal? i = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID"));
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_ACCIDENT>("APRS_HU_ACCIDENT", new EMPLOYEE_SEARCH { ID = i });
                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        protected override bool AddNewButtonClick()
        {
            if (!(grdEmpList.SelectedItems.Count > 0 && grdEmpList.SelectedItems[0].RowType == RowType.Record))
            {
                MessageBox.Show(GetMessageById("MSG_NOCHOISE_EMPLOYEE"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
        protected override bool CancelButtonClick()
        {
            try
            {
                BindDataFromGrid();
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        //chưa sửa store
     
        private void BindDataFromGrid()
        {
            DataSet data = new DataSet();
            data.Locale = System.Globalization.CultureInfo.InvariantCulture;
            decimal? i = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID"));
            var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_ACCIDENT>("PRS_HU_ACCIDENT", new EMPLOYEE_SEARCH { ID = i });
            foreach (var item in lstEmployee)
            {
                var lstDtl = OracleHelper.ExcuteSelectMultiObject<HU_ACCIDENTDETAIL>("APRS_HU_ACCIDENT", new HU_ACCIDENTDETAIL { ACCIDENTID = item.ID });
                item.ACCIDENTDETAIL = lstDtl;
            }
            grdMain.SetDataBinding(lstEmployee, "ID");
            grdMain.RetrieveStructure(true);           
            //grdMain.RetrieveStructure(true);         
            //grdMain.RootTable.ChildTables[0].DataMember  = "EMPLOYEE_IN_ACCIDENT";
        }

        private void grdMain_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGrid();
            //
        }

        private void chkTerminate_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdEmpList_SelectionChanged(object sender, EventArgs e)
        {
          //  DataAccident();
            BindDataFromGrid();
        }        
    }
}
